package cn.doways.api.dto;


import lombok.Data;

/**
 * @ClassName Result
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-08 16:53
 **/
@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;
    
    private Result(T data){
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data=data;
    }
    private Result(String msg){
        this.code = ResultEnum.ERROR.getCode();
        this.data = null;
        this.msg = msg;
    }
    private Result(ResultEnum resultEnum){
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }
    private Result(int code,String msg){
        this.code = code;
        this.msg = msg;
    }
    private Result(int code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }
    
    public static <T> Result<T> success(T data){
        /** 
        * @Description: 成功时调用 
        * @Param: [data] 
        * @return: cn.doways.api.dto.Result<T> 
        * @Author: shellming
        * @Date: 2019-08-08 
        */ 
        return new Result<T>(data);
    } 
    
    public static <T> Result<T> build(ResultEnum resultEnum){
        /** 
        * @Description: 根据返回状态，返回结果 
        * @Param: [resultEnum] 
        * @return: cn.doways.api.dto.Result<T> 
        * @Author: shellming
        * @Date: 2019-08-08 
        */ 
        return new Result<T>(resultEnum);
    }
    
    public static <T> Result<T> build(int code, String msg){
        /** 
        * @Description: 根据Code和Msg返回结果 
        * @Param: [code, msg] 
        * @return: cn.doways.api.dto.Result<T> 
        * @Author: shellming
        * @Date: 2019-08-08 
        */ 
        return new Result<T>(code,msg);
    }
    
    public static <T> Result<T> build(int code,String msg,T data){
        /** 
        * @Description: 根据Code 和 Msg Data 返回结果 
        * @Param: [code, msg, data] 
        * @return: cn.doways.api.dto.Result<T> 
        * @Author: shellming
        * @Date: 2019-08-08 
        */ 
        return new Result<T>(code,msg,data);
    } 
    
    public static <T> Result<T> error(String codeMsg){
        /** 
        * @Description: 失败时调用 
        * @Param: [codeMsg] 
        * @return: cn.doways.api.dto.Result<T> 
        * @Author: shellming
        * @Date: 2019-08-08 
        */ 
        return new Result<T>(codeMsg);
    }
    public static <T> Result<T> error(T data){
        /** 
        * @Description: 根据失败的调用 将返回结果传入 
        * @Param: [data] 
        * @return: cn.doways.api.dto.Result<T> 
        * @Author: shellming
        * @Date: 2019-08-08 
        */ 
        return new Result<T>(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
    }
    public static <T> Result<T> error(String codeMsg,T data){
        /**
         * @Description: 根据失败的调用 将返回结果传入
         * @Param: [data]
         * @return: cn.doways.api.dto.Result<T>
         * @Author: shellming
         * @Date: 2019-08-08
         */
        return new Result<T>(ResultEnum.ERROR.getCode(),codeMsg,data);
    }
    
}
